package com.fanketly.funacg2.ui.components

import android.content.res.Configuration.UI_MODE_NIGHT_YES
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.sp
import com.fanketly.funacg2.ui.theme.BackgroundColor70
import com.fanketly.funacg2.ui.theme.ButtonLightBrown
import com.fanketly.funacg2.ui.theme.FunACG2Theme
import com.fanketly.funacg2.ui.theme.White70
import com.fanketly.funacg2.ui.theme.dp4
@Composable
fun CommonTextField(
    modifier: Modifier = Modifier,
    textValue: MutableState<String>,
    unfocusedColor: Color = BackgroundColor70,
    focusedColor: Color = ButtonLightBrown,
//    backgroundColor: Color = Color.White,
    keyboardType: KeyboardType = KeyboardType.Text,
    label: @Composable (() -> Unit)? = null
) {
    OutlinedTextField(
        singleLine = true,
        value = textValue.value,
        onValueChange = {
            textValue.value = it
        },
        label = label,
        modifier = modifier,
        textStyle = TextStyle(
            fontSize = 20.sp,
        ),
//        colors = TextFieldDefaults.textFieldColors(
//            backgroundColor = Color.White,
//            focusedIndicatorColor = Color.White,
//            unfocusedIndicatorColor = Color.White
//        ),
        colors = OutlinedTextFieldDefaults.colors(
//            backgroundColor = White70,
//            unfocusedBorderColor = unfocusedColor,
//            unfocusedLabelColor = unfocusedColor,
//            focusedBorderColor = focusedColor,
//            focusedLabelColor = focusedColor,
//            cursorColor = focusedColor,
        ),
//keyboardOptions：设置KeyboardType、ImeAction等键盘选项。一些可用的键盘类型是：电子邮件、密码和数字，
// 而重要的 ImeAction 是：Go、Search、Previous、Next 和 Done。
        keyboardOptions = KeyboardOptions.Default.copy(keyboardType = keyboardType)
    )
}
@Composable
fun OutlinedText(
    text: String,
    modifier: Modifier = Modifier,
    padding: Dp = dp4,
    color: Color = MaterialTheme.colorScheme.onPrimaryContainer,
    fontSize: TextUnit = TextUnit.Unspecified,
    fontStyle: FontStyle? = null,
    fontWeight: FontWeight? = null,
    fontFamily: FontFamily? = null,
    letterSpacing: TextUnit = TextUnit.Unspecified,
    textDecoration: TextDecoration? = null,
    textAlign: TextAlign? = null,
    lineHeight: TextUnit = TextUnit.Unspecified,
    overflow: TextOverflow = TextOverflow.Clip,
    softWrap: Boolean = true,
    maxLines: Int = Int.MAX_VALUE,
    onTextLayout: (TextLayoutResult) -> Unit = {},
    style: TextStyle = LocalTextStyle.current,
) {
    Text(
        text = text,
        modifier = modifier
            .background(MaterialTheme.colorScheme.primaryContainer, CardDefaults.outlinedShape)
//            .border(CardDefaults.outlinedCardBorder(), CardDefaults.outlinedShape)
            .padding(padding),
        color = color,
        fontSize = fontSize,
        fontStyle = fontStyle,
        fontWeight = fontWeight,
        fontFamily = fontFamily,
        letterSpacing = letterSpacing,
        textDecoration = textDecoration,
        textAlign = textAlign,
        lineHeight = lineHeight,
        overflow = overflow,
        softWrap = softWrap,
        maxLines = maxLines,
        onTextLayout = onTextLayout,
        style = style
    )
}

@Preview(showBackground = true)
@Preview(showBackground = true, uiMode = UI_MODE_NIGHT_YES)
@Composable
private fun Preview() {
    FunACG2Theme {
        Row(horizontalArrangement = Arrangement.SpaceAround, modifier = Modifier.fillMaxWidth()) {
            repeat(4) {
                OutlinedText(
                    text = "213",
                    modifier = Modifier
                        .padding(dp4)
                )
            }
        }
    }
}